package com.lcfc.attend.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.lcfc.attend.entity.SysUser;
import com.lcfc.attend.entity.vo.ResultData;
import com.lcfc.attend.entity.vo.UserVo;
import com.lcfc.attend.service.SysUserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/login")
public class LoginController {

	@Autowired
	private SysUserService sysUserService;
	
	/**
	 * 登录
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "登录", notes = "登录")
    @PostMapping("/login")
    public ResultData<UserVo> login(@ApiParam(value = "用户名") @RequestParam(required = true) String logincode,
    		@ApiParam(value = "密码") @RequestParam(required = true) String password,
    		HttpServletRequest request) {
		ResultData<UserVo> result = new ResultData<UserVo>();
		List<SysUser> list = sysUserService.login(logincode,password);
		if (list != null && list.size() > 0) {
			SysUser user = list.get(0);
			UserVo uservo  = new UserVo();
			uservo.setUserName(user.getUserName());
			uservo.setUserCode(user.getUserCode());
			uservo.setSign(user.getSign());
			uservo.setLoginCode(user.getLoginCode());
			uservo.setAvatar(user.getAvatar());
			if (!"admin".equals(logincode)) {
				List<Map<String,String>> officeList = sysUserService.queryOfficeByCode(user.getUserCode());
				if (null != officeList && officeList.size() > 0) {
					String codes = "";
					for(Map<String,String> m : officeList) {
						codes += m.get("officeCode") + ",";
			        };
					uservo.setOfficeCode(codes.substring(0, codes.length() - 1));
				}
			}
			result.setCode(0);
			result.setCount(1);
			result.setMsg("登录成功");
			List<UserVo> list2 = new ArrayList<UserVo>();
			list2.add(uservo);
			result.setData(list2);
			return result;
		}
		result.setCode(1);
		result.setCount(0);
		result.setMsg("用户名或密码错误");
		return result;
	}
}
